package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.ShouldGgatherPay;
import com.isunland.managesystem.utils.ToastUtil;

/**
 * 应付、收款统计查询
 *
 * @author wu
 */
public class ShouldCountSearchFragment extends Fragment implements OnCheckedChangeListener {
  public static final String EXTRA_COUNT_TYPE =
      "com.isunland.managesystem.ui.ShouldCountSearchFragment.EXTRA_COUNT_TYPE";
  public static final String EXTRA_COUNT_VALUE =
      "com.isunland.managesystem.ui.ShouldCountSearchFragment.EXTRA_COUNT_VALUE";
  public static final String EXTRA_VALUE =
      "com.isunland.managesystem.ui.ShouldCountSearchFragment.EXTRA_VALUE";

  private CheckBox mcbProjectDeliveryCount;// 合同/订单时间
  private CheckBox mcbCustomerCompanyCount;// 客户单位
  private CheckBox mcbTypeCount;// 类型
  private ShouldGgatherPay mQuery = new ShouldGgatherPay();
  private String mCountQueryType;

  @Override public void onCreate(Bundle savedInstanceState) {
    // 设置返回图标
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    mQuery = (ShouldGgatherPay) getArguments().getSerializable(EXTRA_VALUE);
    mCountQueryType = getArguments().getString(EXTRA_COUNT_TYPE);
    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_count_query, container, false);
    mcbProjectDeliveryCount = (CheckBox) view.findViewById(R.id.cb_projectDelivery_gatherPay);
    mcbCustomerCompanyCount = (CheckBox) view.findViewById(R.id.cb_customerCompany_gatherPay);
    mcbTypeCount = (CheckBox) view.findViewById(R.id.cb_typeCount_gatherPay);

    mcbProjectDeliveryCount.setOnCheckedChangeListener(this);
    mcbCustomerCompanyCount.setOnCheckedChangeListener(this);
    mcbTypeCount.setOnCheckedChangeListener(this);

    if (ShouldCountListFragment.VALUE_GATHER.equalsIgnoreCase(mCountQueryType)) {
      mcbCustomerCompanyCount.setText(R.string.customerCompany);
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.getCountQuery);
      mcbProjectDeliveryCount.setText(R.string.projectDelivery);
      mcbCustomerCompanyCount.setText(R.string.customerCompany);
      mcbTypeCount.setText(R.string.typeCount);
    } else if (ShouldCountListFragment.VALUE_PAY.equalsIgnoreCase(mCountQueryType)) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.payCountQuery);
      mcbCustomerCompanyCount.setText(R.string.contractEnterprise);
      mcbProjectDeliveryCount.setText(R.string.projectDelivery);
      mcbCustomerCompanyCount.setText(R.string.contractEnterprise);
      mcbTypeCount.setText(R.string.typeCount);
    }

    if (!TextUtils.isEmpty(mQuery.getContractDeliveryTime())) {
      mcbProjectDeliveryCount.setChecked(true);
    } else {
      mcbProjectDeliveryCount.setChecked(false);
    }
    if (!TextUtils.isEmpty(mQuery.getCustomer())) {
      mcbCustomerCompanyCount.setChecked(true);
    } else {
      mcbCustomerCompanyCount.setChecked(false);
    }
    if (!TextUtils.isEmpty(mQuery.getCountType())) {
      mcbTypeCount.setChecked(true);
    } else {
      mcbTypeCount.setChecked(false);
    }
    return view;
  }

  @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    switch (buttonView.getId()) {
      // 合同/订单时间
      case R.id.cb_projectDelivery_gatherPay:
        if (isChecked) {
          mQuery.setContractDeliveryTime("OTHER_HELP");
        } else {
          mQuery.setContractDeliveryTime("");
        }
        break;
      // 客户单位
      case R.id.cb_customerCompany_gatherPay:
        if (isChecked) {
          mQuery.setCustomer("PART_ID,PART_NAME");
        } else {
          mQuery.setCustomer("");
        }
        break;
      // 类型
      case R.id.cb_typeCount_gatherPay:
        if (isChecked) {
          mQuery.setConutType("CONTRACT_KIND,CONTRACT_KIND_NAME");
        } else {
          mQuery.setConutType("");
        }
        break;
      default:
        break;
    }
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回图标
      case android.R.id.home:

        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }

      case R.id.menu_item_confirm:
        Intent intent = new Intent();
        String time = mQuery.getContractDeliveryTime();
        String type = mQuery.getCountType();
        String customer = mQuery.getCustomer();
        if (TextUtils.isEmpty(time) && TextUtils.isEmpty(type) && TextUtils.isEmpty(customer)) {
          ToastUtil.show(R.string.atLeastChooseOne);
          return false;
        }

        intent.putExtra(EXTRA_COUNT_VALUE, mQuery);
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();

        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  public static Fragment newInstance(String type, ShouldGgatherPay query) {
    Bundle bundle = new Bundle();
    bundle.putString(EXTRA_COUNT_TYPE, type);
    bundle.putSerializable(EXTRA_VALUE, query);
    ShouldCountSearchFragment fragment = new ShouldCountSearchFragment();
    fragment.setArguments(bundle);
    return fragment;
  }
}
